<?php

class Backend_Flash_Messenger
{

    const TYPE_SUCCESS = 'info';
    const TYPE_ERROR   = 'error';
    const TYPE_INFO    = 'info';

    public function getMessages()
    {
        $session = Zend_Registry::get('session');
        $result  = array();

        if ($session instanceof Zend_Session_Namespace)
        {
            if ($session->messages && is_array($session->messages))
            {
                $result = $session->messages;
                $session->messages = null;
            }
        }

        return $result;
    }

    public function addMessage($text, $class = self::TYPE_INFO)
    {
        $session = Zend_Registry::get('session');

        if ($session instanceof Zend_Session_Namespace)
        {
            if (!$session->messages)
            {
                $session->messages = array();
            }
            $messages = $session->messages;
            $messages[] = array(
                'class' => $class,
                'text'  => $text
            );
            $session->messages = $messages;
        }
    }
}